package com.bw.module_mine

import android.os.Bundle
import android.util.DisplayMetrics
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager

class LogoutDialog(
    private val title: String? = null,  //标题
    private val message: String? = null, //内容
    private val onConfirmClick: (() -> Unit)? = null, //确认回调
    private val onCancelClick: (() -> Unit)? = null,//取消回调
    private val onlyConfirmButton: Boolean = false, //是否只有确定按钮
    private val confirmText: String = "确定",
    private val cancelText: String = "取消",
    private val cancelable: Boolean = false, //点击外部是否可以取消
    private val alignment: Int = Gravity.CENTER, //对齐方式
    private val confirmClickDismiss: Boolean = true //点击确认按钮后是否关闭
) : DialogFragment() {

    companion object{
        const val TAG = "LogoutDialog"
    }

    private lateinit var mParentView: View
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, com.bw.lib_common.R.style.MessageNoticeDialog)
    }

    override fun onStart() {
        super.onStart()
        val window: Window? = dialog!!.window
        val wlp:WindowManager.LayoutParams = window!!.attributes
        val dm = DisplayMetrics()
        activity?.windowManager?.defaultDisplay?.getMetrics(dm)
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        wlp.gravity = Gravity.CENTER
        wlp.width = (dm.widthPixels * 0.72) .toInt()
        window.attributes = wlp
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mParentView = inflater.inflate(R.layout.dialog_logout,container,false)
        return mParentView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(cancelable)
        dialog?.setCanceledOnTouchOutside(cancelable)

        mParentView.findViewById<TextView>(R.id.tv_qx).apply {
            text = cancelText
           if (onlyConfirmButton) {
               visibility = View.GONE
           }
            setOnClickListener {
                onCancelClick?.invoke()
                dismiss()
            }

        }

        mParentView.findViewById<TextView>(R.id.tv_qr).apply {
            text = confirmText
            setOnClickListener {
                onConfirmClick?.invoke()
                if (confirmClickDismiss) {
                    dismiss()
                }
            }
        }

        mParentView.findViewById<TextView>(R.id.ts).text = title
    }

    fun show(fragmentManager: FragmentManager) {
        if (fragmentManager.findFragmentByTag(TAG)?.isVisible == true) {
            return
        }
        this.show(fragmentManager, TAG)
    }
}